package Homework3VideoStore;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public abstract class Customer
{
	protected String name;
	protected int numberOfVideosRented = 0;
	private List<Rental> rentals;
	
	protected Random random;
	
	public Customer()
	{
		rentals = new ArrayList<Rental>();
		random = new Random();
	}
	
	public Rental RentVideos(List<Video> videos, int dayRented)
	{
		numberOfVideosRented += videos.size();
		
		if (numberOfVideosRented > 3)
		{
			System.out.println("ERROR: customer " + name + " has more than 3 videos at one time!");
			System.exit(1);
		}
		
		int nights = numberOfNightsToRent();
		
		Rental rental = new Rental(this, videos, nights, dayRented);
		rentals.add(rental);
		
		return rental;
	}
	
	public List<Rental> ReturnVideos(int currentDay)
	{
		List<Rental> toReturn = new ArrayList<Rental>();
		
		for(int i = 0; i < rentals.size(); i++)
		{
			Rental rental = rentals.get(i);
			if(rental.getNightToReturn() <= currentDay)
			{
				toReturn.add(rental);
				rentals.remove(i);
				
				numberOfVideosRented -= rental.numberOfVideos();
				if (numberOfVideosRented < 0)
				{
					System.out.println("ERROR: customer " + name + "has less than 0 videos!");
					System.exit(1);
				}
			}
		}
		
		return toReturn;
	}
	
	public boolean willComeIn()
	{
		if(numberOfVideosRented < 3)
		{
			if(random.nextInt(2) == 1)
				return true;
		}	
		return false;
	}
	
	public abstract int numberOfVideosToRent();
	public abstract int numberOfNightsToRent();
	public abstract boolean willRent(int numVids);
	
	public String toString()
	{
		return name;
	}
}
